import bus from '../../public/bus'
import moment from 'moment'
import {ElMessage} from 'element-plus'
import store from '../../store'
let chess = require('../../assets/audio/chess.mp3')
let background = require('../../assets/audio/background.mp3')
let win = require('../../assets/audio/win.mp3')
let lost = require('../../assets/audio/lost.mp3')
let flower = require('../../assets/audio/flower.mp3')
let wind = require('../../assets/audio/wind.mp3')
let puzzle = require('../../assets/audio/puzzle.mp3')
let audio = new Audio(chess)
let audioBack = new Audio(background)
let audioWin = new Audio(win)
let audioLost = new Audio(lost)
let audioFlower = new Audio(flower)
let audioWind = new Audio(wind)
let puzzleAudio = new Audio(puzzle)

// 斗地主
let landlordBc = require('../../assets/audio/landlord_bc.wav')
let landlordKing = require('../../assets/audio/landlord_king.wav')
let landlordNo = require('../../assets/audio/landlord_no.wav')
let landlordNoCards = require('../../assets/audio/landlord_noCards.wav')
let landlordOver = require('../../assets/audio/landlord_over.mp3')
let landlordRob = require('../../assets/audio/landlord_rob.wav')
let landlordWin = require('../../assets/audio/landlord_win.wav')

let landlordBcAudio = new Audio(landlordBc)
let landlordKingAudio = new Audio(landlordKing)
let landlordNoAudio = new Audio(landlordNo)
let landlordNoCardsAudio = new Audio(landlordNoCards)
let landlordOverAudio = new Audio(landlordOver)
let landlordWinAudio = new Audio(landlordWin)
let landlordRobAudio = new Audio(landlordRob)


export const webServer = (email:any)=>{
	if(!(store.state.userInfoX && store.state.userInfoX.token)){
		return
	}
	let websocket = process.env.NODE_ENV === "production" ?new WebSocket(`ws://101.43.228.243:9003?user=${email}`):new WebSocket(`ws://127.0.0.1:9003?user=${email}`);
	websocket.onopen = function () {
		console.log('websocket open');
		store.commit('battleInfoMain',null)
	}
	websocket.onclose = function () {
		if(store.state.userInfoX && store.state.userInfoX.token){
			store.commit('sockerMain',webServer(store.state.userInfoX?.account))
		}else{
			bus.emit('clear')
		}
	}
	websocket.onmessage = function (ev) {
		let data = JSON.parse(ev.data)
		console.log(data)
		if(!store.state?.userInfoX?.token){
			return
		}
		if(data.infoType == 'invite'){
			bus.emit('inviteInfo',ev.data)
		}else if(data.infoType == 'newLine' && data.to == store.state.userInfoX?.account){
			bus.emit('update',{
				user:data.user,
				type:data.type
			})
		}else if(data.infoType == 'game' && data.to == store.state.userInfoX?.account){
			bus.emit('game',{
				user:data.user,
				type:data.type
			})
		}else if(data.infoType == 'playing' && data.to == store.state.userInfoX?.account){
			bus.emit('playing',{
				user:data.user,
				type:data.type
			})
		}else if(data.infoType == 'lost' && data.lostUser !== store.state.userInfoX?.account){
			bus.emit('lost',{
				user:data.lostUser,
				type:data.infoType
			})
		}else if(data.infoType == 'topOff' && data.to == store.state.userInfoX?.account){
			store.commit("logoutMain",1)
		}else if(data.infoType == 'progress' && data.to == store.state.userInfoX?.account){
			bus.emit("progress",data.progress)
		}else{
			bus.emit('info',ev.data)
		}
		
	}
	return websocket
}
// 断线重新连接
export const reconnection = (info:any)=>{
	let count = 0 as any
	return function t(){
		if(count > 5){
			ElMessage({
				message: "连接服务失败,请重新操作！",
				type: 'success',
			})
			return
		}
		count++
		store.commit('sockerMain',webServer(store.state.userInfoX&&store.state.userInfoX?.account))
		setTimeout(()=>{
			if(!store.state?.webSocketX || store.state?.webSocketX?.readyState == 3 || store.state?.webSocketX?.readyState == 0){
				t()
			}else{
				count = null
				ElMessage({
					message: "连接成功,请重新操作！",
					type: 'success',
				})
				// store.state?.webSocketX.send(JSON.stringify(info))
			}
		},3000)
	}
}
// 发送信息
export const sendMessage = (info:object)=>{
	console.log(info)
	let websocket = store.state?.webSocketX
	let baseInfo = {
		avatar:store.state.userInfoX&&store.state.userInfoX?.avatar,
		nickName:store.state.userInfoX&&store.state.userInfoX?.nickName,
		from:store.state.userInfoX&&store.state.userInfoX?.account,
		creatTime:moment().format()
	}
	let sendInfo = Object.assign(baseInfo,info)
	let t = reconnection(sendInfo)
	if(websocket&&websocket?.readyState !== 3){
		try{
			console.log(94,sendInfo)
			websocket.send(JSON.stringify(sendInfo))
		}catch(err:any){
			t()
			ElMessage.error("发送失败")
			return false
		}
		return true
	}else{
		t()
		ElMessage.error("发送失败")
		return false
	}
}
// 播放音效
export const playAudio = (type:number)=>{
	if(type === 1){
		// 下棋声
		audio.play();
	}else if(type === 2){
		// 游戏背景音乐
		audioBack.loop = true
		audioBack.play()
		
	}else if(type === 3){
		audioBack.loop = false
		audioBack.pause()
		landlordBcAudio.loop = false
		landlordBcAudio.pause()
	}else if(type === 4){
		// 游戏赢了
		audioWin.play()
	}else if(type === 5){
		// 游戏输了
		audioLost.play()
	}else if(type === 6){
		// 我等的花都谢了
		audioFlower.play()
	}else if(type === 7){
		// 面对疾风吧
		audioWind.play()
	}else if(type === 8){
		// 拼图声音
		puzzleAudio.play()
	}else if(type === 9){
		// 斗地主背景
		landlordBcAudio.loop = true
		landlordBcAudio.play()
	}else if(type === 10){
		// 斗地主王炸
		landlordKingAudio.play()
	}else if(type === 11){
		// 斗地主赢牌
		landlordWinAudio.play()
	}else if(type === 12){
		// 斗地主输牌
		landlordOverAudio.play()
	}else if(type === 13){
		// 斗地主不出
		landlordNoCardsAudio.play()
	}else if(type === 14){
		// 斗地主不抢
		landlordNoAudio.play()
	}else if(type === 15){
		// 斗地主抢地主
		landlordRobAudio.play()
	}
	
	
	
	
	
	
	
	
}


